package mail_operations;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import data.ConfigInfo;
import data.Reference;

import GUIMain.GUIFocusManager;

public class AdvancedOptionDialog {
	private static boolean initialized = false;
	private static GridBagConstraints gbc;
	private static JDialog dialog;
	private static JButton cancel;
	private static JButton submit;
	private static JScrollPane jsp;
	private static JTextField serverAddress;
	private static JTextField senderEmail;
	private static JCheckBox sendToMyself;
	private static JTextArea additionalEmailText;
	
	private static void initialize(){
		if(!initialized){
			initialized = true;
			dialog = new JDialog();
			gbc = new GridBagConstraints();
			dialog.setLayout(new GridBagLayout());
			dialog.setTitle("Mailing Options");
			serverAddress = new JTextField();
			serverAddress.setPreferredSize(new Dimension(150,25));
			senderEmail = new JTextField();
			senderEmail.setPreferredSize(new Dimension(150,25));
			cancel = new JButton("Cancel");
			submit = new JButton("Submit");
			additionalEmailText = new JTextArea();
			jsp = new JScrollPane(additionalEmailText);
			sendToMyself = new JCheckBox("Copy Myself");
			
			gbc.insets.top = 2;
			gbc.insets.bottom = 2;
			gbc.insets.left = 2;
			gbc.insets.right = 2;
			gbc.gridwidth =1;
			gbc.gridheight =1;
			gbc.gridx =0;
			gbc.gridy = 0;
			dialog.add(new JLabel("Server Address"),gbc);
			gbc.gridx = 1;
			dialog.add(serverAddress,gbc);
			gbc.gridx = 0;
			gbc.gridy = 1;
			dialog.add(new JLabel("Sender Email"),gbc);
			gbc.gridx = 1;
			dialog.add(senderEmail,gbc);
			gbc.gridy = 2;
			gbc.gridx = 1;
			dialog.add(sendToMyself,gbc);
			gbc.gridwidth =2;
			gbc.gridx = 0;
			gbc.gridy = 3;
			dialog.add(new JLabel("Copy Additional Addressees (1 per line)"),gbc);
			gbc.gridy = 4;
			additionalEmailText.setPreferredSize(new Dimension(250,200));
			dialog.add(jsp,gbc);
			
			gbc.gridwidth = 1;
			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			gbc.gridx =0;
			gbc.gridy = 0;
			buttonPanel.add(cancel,gbc);
			gbc.gridx = 1;
			buttonPanel.add(submit,gbc);
			
			gbc.gridwidth =2;
			gbc.gridx = 0;
			gbc.gridy = 5;
			dialog.add(buttonPanel,gbc);
			attachListeners();
			dialog.pack();
			dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(false);
			dialog.setResizable(false);
		}
	}
	public static void open(){
		if(!initialized)
			initialize();
		serverAddress.setText(ConfigInfo.SERVER_ADDRESS);
		serverAddress.setCaretPosition(0);
		senderEmail.setText(ConfigInfo.SENDER_EMAIL);
		senderEmail.setCaretPosition(0);
		sendToMyself.setSelected(ConfigInfo.SEND_TO_SELF);
		StringBuffer content = new StringBuffer("");
		for(int i=0;i<ConfigInfo.ADDITIONAL_EMAIL_LIST.size();i++){
			content.append(ConfigInfo.ADDITIONAL_EMAIL_LIST.get(i) + "\n");
		}
		additionalEmailText.setText(content.toString());
		
		GUIFocusManager.add(dialog);
	}
	private static void attachListeners(){
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		});
		submit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//Check for valid data in these fields
				if(serverAddress.getText().matches("\\s+")){
					JOptionPane.showMessageDialog(null,"Server Address Cannot be blank!");
					serverAddress.requestFocus();
					return;
				}else if(senderEmail.getText().matches("\\s+")){
					JOptionPane.showMessageDialog(null,"Sender email cannot be blank!");
					senderEmail.requestFocus();
					return;
				}
				if(additionalEmailText.getText().length()>0){
					if(additionalEmailText.getText().length()>0){
						String[] emails = additionalEmailText.getText().split("\n");
						for(int i=0;i<emails.length;i++){
							String email = emails[i];
							if(!email.matches(Reference.EMAIL_REGEX)){
								JOptionPane.showMessageDialog(null, "Invalid Email Format!");
								additionalEmailText.requestFocus();
								return;
							}
						}
						ConfigInfo.ADDITIONAL_EMAIL_LIST.clear();
						for(int i=0;i<emails.length;i++){
							ConfigInfo.ADDITIONAL_EMAIL_LIST.add(emails[i]);
						}
					}
				}else{
					ConfigInfo.ADDITIONAL_EMAIL_LIST.clear();
				}
				ConfigInfo.SERVER_ADDRESS = serverAddress.getText();
				ConfigInfo.SENDER_EMAIL = senderEmail.getText();
				ConfigInfo.SEND_TO_SELF = sendToMyself.isSelected();
				GUIFocusManager.remove();
			}
		});
	}
}
